<template>
  <div>
    <el-dialog title="发布招标" :visible="dialogVisible" @close="handleClose">
      <el-form
        ref="AddInvite"
        :model="AddInviteData"
        :rules="rules"
        size="small"
        label-width="120px"
      >
        <el-form-item label="标的名称" prop="title">
          <el-input v-model.trim="AddInviteData.title" placeholder="标的名称" />
        </el-form-item>
        <el-form-item label="项目名" prop="projectName">
          <el-input v-model.trim="AddInviteData.projectName" placeholder="项目名" />
        </el-form-item>
        <el-form-item label="项目号" prop="projectSerial">
          <el-input v-model.trim="AddInviteData.projectSerial" placeholder="项目号" />
        </el-form-item>
        <el-form-item label="项目概况" prop="summary">
          <el-input v-model.trim="AddInviteData.summary" placeholder="项目概况" />
        </el-form-item>
        <el-form-item label="发布人" prop="publisher">
          <el-input
            v-model.trim="AddInviteData.publisher"
            :readonly="isReadOnly"
            placeholder="发布人"
          />
        </el-form-item>
        <el-form-item label="发布人邮箱" prop="publisherMailAddr">
          <el-input
            v-model.trim="AddInviteData.publisherMailAddr"
            :readonly="isReadOnly"
            placeholder="发布人邮箱"
          />
        </el-form-item>
        <el-form-item label="部门领导" prop="deptLeader">
          <el-input v-model.trim="AddInviteData.deptLeader" placeholder="部门领导" />
        </el-form-item>
        <el-form-item label="部门领导邮箱" prop="deptLeaderMailAddr">
          <el-input v-model.trim="AddInviteData.deptLeaderMailAddr" placeholder="部门领导邮箱" />
        </el-form-item>
        <el-form-item label="分组" prop="supplyGroup">
          <!-- <el-input v-model.trim="AddInviteData.supplyGroup" placeholder="分组" /> -->
          <el-select v-model="AddInviteData.supplyGroup" style="width: 100%;">
            <el-option v-for="(item,i) in SupplyGroupList" :key="i" :value="item" :label="item" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" plain size="medium" @click="submit">{{ $t('button.Create') }}</el-button>
          <!-- <el-button size="medium" @click="handleClose">{{ $t('button.Cancel') }}</el-button> -->
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { InvitePublish, SendInviteMail, InviteGetList, GetSupplyGroup } from '@/api/SRM'
import elDragDialog from '@/components/el-drag-dialog' // base on element-ui
import { Message } from 'element-ui'
import { getUserInfo } from '@/utils/auth.js'

export default {
  name: 'AddInviteDialog',
  directives: { elDragDialog },
  props: {
    addInviteDialogFlag: {
      type: Boolean,
      default: false,
      required: true
    }
  },
  data() {
    return {
      dialogVisible: false,
      isReadOnly: false,
      AddInviteData: {
        title: '',
        projectName: '',
        projectSerial: '',
        summary: '',
        publisher: '',
        inviteInfoMailAddr: '',
        deptLeader: '',
        deptLeaderMailAddr: '',
        supplyGroup: ''
      },
      // 供应商分组
      SupplyGroupList: [],
      // 表单校验
      rules: {
        title: [
          { required: true, message: '必填哦！', trigger: ['blue', 'change'] }
        ],
        projectName: [
          { required: true, message: '必填哦！', trigger: ['blue', 'change'] }
        ],
        projectSerial: [
          { required: true, message: '必填哦！', trigger: ['blue', 'change'] }
        ],
        summary: [
          { required: true, message: '必填哦！', trigger: ['blue', 'change'] }
        ],
        publisher: [
          { required: true, message: '必填哦！', trigger: ['blue', 'change'] }
        ],
        publisherMailAddr: [
          { required: true, message: '必填哦！', trigger: ['blue', 'change'] }
        ],
        deptLeader: [
          { required: true, message: '必填哦！', trigger: ['blue', 'change'] }
        ],
        deptLeaderMailAddr: [
          { required: true, message: '必填哦！', trigger: ['blue', 'change'] }
        ],
        supplyGroup: [
          { required: true, message: '请选择', trigger: ['blue'] }
        ]
      }
    }
  },
  watch: {
    addInviteDialogFlag() {
      this.dialogVisible = this.addInviteDialogFlag
    }
  },
  mounted() {},
  async created() {
    const userInfo = getUserInfo()
    if (userInfo != null) {
      this.isReadOnly = true
      this.AddInviteData.publisher = userInfo.userName
      this.AddInviteData.publisherMailAddr = userInfo.mailAccount
    } else {
      this.isReadOnly = false
    }
    const res = await GetSupplyGroup()
    this.SupplyGroupList = res.data.data
  },
  methods: {
    async submit() {
      this.$refs.AddInvite.validate(async valid => {
        if (valid) {
          try {
            await InvitePublish(this.AddInviteData)
            Message.success('创建成功')
            this.dialogVisible = false
            // this.$confirm('是否通知供应商发布了招标信息？', '提示', {
            //   confirmButtonText: '确定',
            //   cancelButtonText: '稍后提醒',
            //   type: 'warning'
            // }).then(async() => {
            //   console.log(this.AddInviteData)
            //   const a = await InviteGetList()
            //   let nowid
            //   a.data.data.forEach(item => {
            //     if (item.title === this.AddInviteData.title && item.projectName === this.AddInviteData.projectName) {
            //       nowid = item
            //     }
            //   })
            //   console.log(nowid)
            //   await SendInviteMail({ id: nowid.id })
            //   Message.success('操作成功')
            // })
          } catch (error) {
            console.log('AddInvite->submit', error.message)
            Message.error('创建失败')
          }
        } else {
          Message.error('数据输入有误')
          return false
        }
      })
    },
    handleClose() {
      this.$emit('updateValue')
      this.$emit('update:addInviteDialogFlag', false)
    }
  }
}
</script>

<style>
</style>
